<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Index extends Common {
		
	public function index(){
		return $this->fetch('template/'.config('template.default_them').'/index.html');
	}
	
	public function seach(){
		$data = request()->param();
		$keyword = $data['key'];
		if(empty($keyword)){
			$this->error('必须输入关键字');
		}
		$title= '%'.$data['key'].'%';
		
		$data = db('model')->where(['id'=>$data['model_id']])->find();
		$rest = db($data['table_name'])->where('title|description','like',$title)->select();
		
		if(!empty($rest)){
			foreach($rest as $key=>$v){
				$rest[$key]['title'] = preg_replace("/($keyword)/","<b style='color:#e2a371'>{$keyword}</b>",$rest[$key]['title']);
				$rest[$key]['description'] = preg_replace("/($keyword)/","<b style='color:#e2a371'>{$keyword}</b>",$rest[$key]['description']);
			}
		}
		// p($rest);
		$this->assign('rest',$rest);
		return $this->fetch('template/'.config('template.default_them').'/seach.html');
	}
	
	public function form(){
		return $this->fetch('template/'.config('template.default_them').'/form.html');
	}
	
	public function category(){
		$data = request()->param();
		$limit = request()->param('limit')?request()->param('limit'):10;
		$page = request()->param('page')?request()->param('page'):1;
		$start = ($page-1)*$limit;
		$rest  = db('category')
				->alias('a')
				->join('models b','a.model_id = b.id')
				->where(['a.id'=>$data['cid']]) 
				->order('a.sort asc')
				->field('a.id,a.pid,a.cat_pic,a.type,a.name,a.sort,a.model_id,a.status,a.template_category,a.template_list,a.template_show,b.table_name')
				->find();
		
		$childMenu = db('category')->where(['pid'=>$data['cid']])->order('sort')->select();
		foreach($childMenu as $k=>$v){
			$childMenu[$k]['url'] = "lists.html?cid=".$v['id'];
		}
		$rest['childMenu'] = $childMenu;
		// p($rest);die;
		$d = $this->getCateData($data['cid']);
		$tablec = $rest['table_name'].'_data';
		$data = db($rest['table_name'])
				->alias('a')
				->join($tablec." b","a.did=b.id")
				->where(['status'=>1])
				->where('cid','in',$d)
				->limit($start,$limit)
				->select();
		$count = db($rest['table_name'])
				->alias('a')
				->join($tablec." b","a.did=b.id")
				->where(['status'=>1])
				->where('cid','in',$d)
				->count();
		// p($data);die;
		foreach($data as $k=>$v){
			$data[$k]['url'] = "page.html?cid=".$v['cid'].'&aid='.$v['aid'];
		}
		// p($data);die;
		$p = ceil($count/10);
		// p($p);die;
		$this->assign('data',$data);
		$this->assign('count',$count);
		$this->assign('page',$p);
		$this->assign('info',$rest);
		if($rest['type'] == 3){
			return $this->fetch('template/'.config('template.default_them').'/'.$rest['template_show']);
		}
		return $this->fetch('template/'.config('template.default_them').'/'.$rest['template_category']);
	}
	
	public function lists(){
		$data = request()->param();
		$rest  = db('category')
				->alias('a')
				->join('models b','a.model_id = b.id')
				->where(['a.id'=>$data['cid']]) 
				->order('a.sort asc')
				->field('a.id,a.pid,a.cat_pic,a.type,a.name,a.sort,a.model_id,a.status,a.template_list,a.template_show,b.table_name')
				->find();
		
		$c = db('category')->where(['id'=>$rest['pid']])->find();
		// p($c);die;
		$childMenu = db('category')->where(['pid'=>$rest['pid']])->order('sort')->select();
		foreach($childMenu as $k=>$v){
			$childMenu[$k]['url'] = "lists.html?cid=".$v['id'];
		}
		$c['childMenu'] = $childMenu;
		// p($rest);die;
		$tablec = $rest['table_name'].'_data';
		$data = db($rest['table_name'])
				->alias('a')
				->join($tablec." b","a.did=b.id")
				->where(['cid'=>$rest['id'],'status'=>1])
				->select();
		// p($data);die;
		foreach($data as $k=>$v){
			$data[$k]['url'] = "page.html?cid=".$v['cid'].'&aid='.$v['aid'];
		}
		// p($data);die;
		$this->assign('data',$data);
		$this->assign('info',$c);
		if($rest['type'] == 3){
			return $this->fetch('template/'.config('template.default_them').'/'.$rest['template_show']);
		}
		return $this->fetch('template/'.config('template.default_them').'/'.$rest['template_list']);
	}
	public function page(){
		$data = request()->param();
		$rest  = db('category')
				->alias('a')
				->join('models b','a.model_id = b.id')
				->where(['a.id'=>$data['cid']]) 
				->order('a.sort asc')
				->field('a.id,a.pid,a.cat_pic,a.name,a.type,a.sort,a.model_id,a.status,a.template_show,b.table_name')
				->find();
		if($rest['type'] == 3){
			$tablec = $rest['table_name'].'_data';
			$data = db($rest['table_name'])
					->alias('a')
					->join($tablec." b","a.did=b.id")
					->where(['cid'=>$rest['id'],'status'=>1])
					->find();
		}else{
			$tablec = $rest['table_name'].'_data';
			$data = db($rest['table_name'])
					->alias('a')
					->join($tablec." b","a.did=b.id")
					->where(['aid'=>$data['aid'],'status'=>1])
					->find();
		}
		$beforePage = $this->beforePage($rest);
		$afterPage = $this->afterPage($rest);

		$this->assign('data',$data);
		$this->assign('before',$beforePage);
		$this->assign('after',$afterPage);
		return $this->fetch('template/'.config('template.default_them').'/'.$rest['template_show']);
	}
	
	//分页
	public function pagination(){
		$data = request()->param();
	}
	
	//下一篇
	public function beforePage($data){
		$aid = request()->param('aid');
		$result = Db::name($data['table_name'])->where('aid','<',$aid)->where(['cid'=>$data['id']])->limit(1)->order('aid','desc')->find();
		if(empty($result)){
			$result   = "上一篇:<a href='javascript:;'>没有了</a>";
		}else{
			$result = "上一篇:<a href='page.html?cid=".$data['id']."&aid=".$result['aid']."'>".$result['title']."</a>";
		}
		return $result;
	}
	//上一篇
	public function afterPage($data){
		$aid = request()->param('aid');
		$result = Db::name($data['table_name'])->where('aid','>',$aid)->where(['cid'=>$data['id']])->limit(1)->find();
		
		if(empty($result)){
			$result   = "下一篇:<a href='javascript:;'>没有了</a>";
		}else{
			$result = "下一篇:<a href='page.html?cid=".$data['id']."&aid=".$result['aid']."'>".$result['title']."</a>";
		}
		return $result;
	}
	
	
	
	
	//处理所属分类
	public function getCateData($id)
	{
		$data = db('category')->select();
	    //1.先找出所有子集
	    $ids = $this->getSon($data, $id);
	    //2.把自己追加进去
	    $ids[] = $id;
	    //3.获取除了自己和自己子集的全部数据
	    // $map['id'] = array('not in', $ids);
	    // $data = db('category')->where($map)->select();
		// var_dump($ids);die;
	    return $ids;
	}
	
	//递归找子集
	public function getSon($data, $id)
	{
	    static $temp = [];
	    foreach ($data as $k => $v) {
	        if ($v['pid'] == $id) {//这个时候说明你是$cid的儿子
	            $temp[] = $v['id'];
	            $this->getSon($data, $v['id']);
	        }
	    }
	    return $temp;
	}
}